/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TransaksiPenjualan.java
 *
 * Created on Aug 11, 2010, 11:28:41 PM
 */
package com.ivanbiz.gui;

import com.ivanbiz.helper.RightRenderer;
import com.ivanbiz.dao.BarangDAO;
import com.ivanbiz.dao.PembelianDAO;
import com.ivanbiz.dao.SupplierDAO;
import com.ivanbiz.helper.CenterRenderer;
import com.ivanbiz.helper.RandomList;
import com.ivanbiz.helper.ServiceHelper;
import com.ivanbiz.impl.BarangDAOImpl;
import com.ivanbiz.impl.PembelianDAOImpl;
import com.ivanbiz.impl.SupplierDAOImpl;
import com.ivanbiz.model.Barang;
import com.ivanbiz.model.ItemPembelian;
import com.ivanbiz.model.Pembelian;
import com.ivanbiz.model.Supplier;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Aprisma
 */
public class TransaksiPembelian extends javax.swing.JInternalFrame {

    ServiceHelper serviceHelper = new ServiceHelper();
    PembelianDAO pembelianDAO = new PembelianDAOImpl();
    SupplierDAO supplierDAO = new SupplierDAOImpl();
    BarangDAO barangDAO = new BarangDAOImpl();
    List<ItemPembelian> listItemPembelian = new ArrayList<ItemPembelian>();
    List<Supplier> listSupplier = new ArrayList<Supplier>();
    List<Barang> listBarang = new ArrayList<Barang>();
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yyyy");
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("MMyy");
    double totals = 0;

    /** Creates new form TransaksiPenjualan */
    public TransaksiPembelian() {
        initComponents();
        updateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupPilihan = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        labelTanggal = new javax.swing.JLabel();
        textTanggal = new javax.swing.JTextField();
        labelKode = new javax.swing.JLabel();
        textKode = new javax.swing.JTextField();
        labelStatusPembayaran = new javax.swing.JLabel();
        boxTunai = new javax.swing.JCheckBox();
        boxKredit = new javax.swing.JCheckBox();
        labelSupplier = new javax.swing.JLabel();
        comboSupplier = new javax.swing.JComboBox();
        textJatuhTempo = new javax.swing.JTextField();
        labelJatuhTempo = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        buttonProses = new javax.swing.JButton();
        textTotal = new javax.swing.JTextField();
        labelPT = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableItemPembelian = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        comboBarang = new javax.swing.JComboBox();
        textHargaBeli = new javax.swing.JTextField();
        labelHargaBeli = new javax.swing.JLabel();
        labelJumlah = new javax.swing.JLabel();
        textJumlah = new javax.swing.JTextField();
        buttonTambah = new javax.swing.JButton();
        buttonKurang = new javax.swing.JButton();
        textCari = new javax.swing.JTextField();
        comboCari = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setBorder(new org.jdesktop.swingx.border.DropShadowBorder());
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Transaksi Pembelian");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Pembelian"));

        labelTanggal.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTanggal.setText("Tanggal");

        textTanggal.setEditable(false);
        textTanggal.setFont(new java.awt.Font("Tahoma", 1, 11));
        textTanggal.setText("jTextField1");

        labelKode.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelKode.setText("Kode");

        textKode.setEditable(false);
        textKode.setFont(new java.awt.Font("Tahoma", 1, 11));
        textKode.setText("jTextField2");

        labelStatusPembayaran.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelStatusPembayaran.setText("Status Pembayaran");

        groupPilihan.add(boxTunai);
        boxTunai.setFont(new java.awt.Font("Tahoma", 1, 11));
        boxTunai.setSelected(true);
        boxTunai.setText("Tunai");
        boxTunai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxTunaiActionPerformed(evt);
            }
        });

        groupPilihan.add(boxKredit);
        boxKredit.setFont(new java.awt.Font("Tahoma", 1, 11));
        boxKredit.setText("Kredit");
        boxKredit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxKreditActionPerformed(evt);
            }
        });

        labelSupplier.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelSupplier.setText("Pemasok");

        comboSupplier.setFont(new java.awt.Font("Tahoma", 1, 11));
        comboSupplier.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        textJatuhTempo.setEditable(false);
        textJatuhTempo.setFont(new java.awt.Font("Tahoma", 1, 11));
        textJatuhTempo.setText("jTextField1");

        labelJatuhTempo.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelJatuhTempo.setText("Jatuh Tempo");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelStatusPembayaran)
                    .addComponent(labelTanggal)
                    .addComponent(labelKode)
                    .addComponent(labelSupplier))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(boxKredit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(labelJatuhTempo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textJatuhTempo, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(textKode, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                    .addComponent(textTanggal, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                    .addComponent(comboSupplier, javax.swing.GroupLayout.Alignment.LEADING, 0, 198, Short.MAX_VALUE)
                    .addComponent(boxTunai, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTanggal)
                    .addComponent(textTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textKode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelKode))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSupplier)
                    .addComponent(comboSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelStatusPembayaran)
                    .addComponent(boxTunai))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textJatuhTempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelJatuhTempo)
                    .addComponent(boxKredit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Total"));

        buttonProses.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonProses.setText("Prosess");
        buttonProses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonProsesActionPerformed(evt);
            }
        });

        textTotal.setEditable(false);
        textTotal.setFont(new java.awt.Font("Tahoma", 1, 18));
        textTotal.setText("jTextField2");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonProses)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(buttonProses, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textTotal, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        labelPT.setFont(new java.awt.Font("Tahoma", 1, 36));
        labelPT.setText("PT BERGUNA JAYA");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Tabel Pembelian"));

        tableItemPembelian.setAutoCreateRowSorter(true);
        tableItemPembelian.setFont(new java.awt.Font("Tahoma", 1, 11));
        tableItemPembelian.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableItemPembelian);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Barang");

        comboBarang.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        comboBarang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        textHargaBeli.setFont(new java.awt.Font("Tahoma", 1, 11));
        textHargaBeli.setText("jTextField1");

        labelHargaBeli.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelHargaBeli.setText("Harga Beli");

        labelJumlah.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelJumlah.setText("Jumlah");

        textJumlah.setFont(new java.awt.Font("Tahoma", 1, 11));
        textJumlah.setText("jTextField3");

        buttonTambah.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonTambah.setText("+");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });

        buttonKurang.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonKurang.setText("-");
        buttonKurang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonKurangActionPerformed(evt);
            }
        });

        textCari.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        textCari.setText("jTextField1");
        textCari.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textCariKeyReleased(evt);
            }
        });

        comboCari.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        comboCari.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 742, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboBarang, 0, 320, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelHargaBeli)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(textHargaBeli, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelJumlah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(textJumlah, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonTambah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonKurang))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(comboCari, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCari, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(comboBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonKurang)
                        .addComponent(buttonTambah))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(textJumlah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(labelJumlah)
                        .addComponent(textHargaBeli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(labelHargaBeli)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setText("Jln. Jembatan Selatan no 470 Panglima Polim Jakarta Selatan ");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setText("Telp : 08174820800");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelPT)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(labelPT)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-550)/2, 800, 550);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        try {
            Barang barang = (Barang) listBarang.get(comboBarang.getSelectedIndex());
            if (textJumlah.getText().isEmpty() || textHargaBeli.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, "Harga Jual atau Jumlah Belum diisi !!!");
            } else if (Float.parseFloat(textJumlah.getText()) <= 0.0) {
                JOptionPane.showMessageDialog(null, "Jumlah Tidak Boleh Kurang atau Sama dengan 0 !!!");
            } else {
                ItemPembelian itemPembelian = new ItemPembelian();
                itemPembelian.setBarang(barang);
                itemPembelian.setHarga(Double.parseDouble(textHargaBeli.getText()));
                itemPembelian.setJumlah(Float.parseFloat(textJumlah.getText()));
                itemPembelian.setCreateDate(ServiceHelper.getDateNow());
                listItemPembelian.add(itemPembelian);
                updateTableItemNota();
                updateTotal();
                textHargaBeli.setText("");
                textJumlah.setText("");
//                boolean cek = true;
//                for (int x = 0; x < listItemPembelian.size(); x++) {
//                    ItemPembelian itemPembelianTemporary = (ItemPembelian) listItemPembelian.get(x);
//                    if (barang.getId() == itemPembelianTemporary.getBarang().getId()) {
//                        cek = false;
//                        JOptionPane.showMessageDialog(null, "Barang Sudah Dipilih !!! ");
//                    }
//                }
//                if (cek == true) {
//                    listItemPembelian.add(itemPembelian);
//                    updateTableItemNota();
//                    updateTotal();
//                    textHargaBeli.setText("");
//                    textJumlah.setText("");
//                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Format Harga dan Jumlah Harus Angka !!!");
            Logger.getLogger(TransaksiPembelian.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonTambahActionPerformed

    private void buttonKurangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonKurangActionPerformed
        listItemPembelian.remove(tableItemPembelian.getSelectedRow());
        updateTableItemNota();
        updateTotal();
    }//GEN-LAST:event_buttonKurangActionPerformed

    private void buttonProsesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonProsesActionPerformed
        try {
            Pembelian pembelian = new Pembelian();
            pembelian.setCreateDate(ServiceHelper.getDateNow());
            pembelian.setKode(textKode.getText().toUpperCase());
            pembelian.setSupplier((Supplier) listSupplier.get(comboSupplier.getSelectedIndex()));
            pembelian.setItemPembelian(listItemPembelian);
            if (pembelian.getItemPembelian().isEmpty()) {
                JOptionPane.showMessageDialog(null, "Barang Belum Dipilih !!!");
            } else if (boxTunai.isSelected()) {
                pembelian.setStatusPembayaran(boxTunai.getText());
                pembelianDAO.saveTransaksiPembelian(pembelian, totals, 0l);
                listItemPembelian.clear();
                totals = 0;
                updateData();
            } else if (boxKredit.isSelected()) {
                if (textJatuhTempo.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "Jatuh Tempo Belum Diisi !!!");
                } else {
                    pembelian.setStatusPembayaran(boxKredit.getText());
                    pembelianDAO.saveTransaksiPembelian(pembelian, totals, Long.parseLong(textJatuhTempo.getText().toUpperCase()));
                    listItemPembelian.clear();
                    totals = 0;
                    updateData();
                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Format Jatuh Tempo Harus Angka Atau Data Gagal Di simpan");
            Logger.getLogger(TransaksiPembelian.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonProsesActionPerformed

    private void boxTunaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxTunaiActionPerformed
        textJatuhTempo.setEditable(false);
    }//GEN-LAST:event_boxTunaiActionPerformed

    private void boxKreditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxKreditActionPerformed
        textJatuhTempo.setEditable(true);
    }//GEN-LAST:event_boxKreditActionPerformed

    private void textCariKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textCariKeyReleased
        try {
            listBarang = barangDAO.getDataByLike(Barang.class, comboCari.getSelectedItem().toString(), textCari.getText());
            updateComboBarang();
        } catch (Exception ex) {
            Logger.getLogger(TransaksiPembelian.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_textCariKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox boxKredit;
    private javax.swing.JCheckBox boxTunai;
    private javax.swing.JButton buttonKurang;
    private javax.swing.JButton buttonProses;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JComboBox comboBarang;
    private javax.swing.JComboBox comboCari;
    private javax.swing.JComboBox comboSupplier;
    private javax.swing.ButtonGroup groupPilihan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelHargaBeli;
    private javax.swing.JLabel labelJatuhTempo;
    private javax.swing.JLabel labelJumlah;
    private javax.swing.JLabel labelKode;
    private javax.swing.JLabel labelPT;
    private javax.swing.JLabel labelStatusPembayaran;
    private javax.swing.JLabel labelSupplier;
    private javax.swing.JLabel labelTanggal;
    private javax.swing.JTable tableItemPembelian;
    private javax.swing.JTextField textCari;
    private javax.swing.JTextField textHargaBeli;
    private javax.swing.JTextField textJatuhTempo;
    private javax.swing.JTextField textJumlah;
    private javax.swing.JTextField textKode;
    private javax.swing.JTextField textTanggal;
    private javax.swing.JTextField textTotal;
    // End of variables declaration//GEN-END:variables

    private void updateComboBarang() {
        Object data[] = new Object[listBarang.size()];
        int x = 0;
        for (Barang barang : listBarang) {
            data[x] = barang.getKode() + " (" + barang.getNama() + " Rp " + numberFormat.format(barang.getHarga()) + ",00" + ")" + " stock " + barang.getStock() + " / " + barang.getSatuan().getNama();
            x++;
        }
        comboBarang.setModel(new DefaultComboBoxModel(data));
    }

    private void updateComboBarangByFind(String nama) {
        Iterator iter = this.listBarang.iterator();
        while (iter.hasNext()) {
            Barang barang = (Barang) iter.next();
            if (!barang.getNama().startsWith(nama)) {
                iter.remove();
            }
        }
        Object data[] = new Object[listBarang.size()];
        int x = 0;
        for (Barang barang : listBarang) {
            data[x] = barang.getKode() + " (" + barang.getNama() + " Rp " + numberFormat.format(barang.getHarga()) + ",00" + ")" + " stock " + barang.getStock() + " / " + barang.getSatuan().getNama();
            x++;
        }
        comboBarang.setModel(new DefaultComboBoxModel(data));
    }

    private void updateComboCari() {
        Object[] dataClass = serviceHelper.getPropertyClass(Barang.class);
        Object[] dataSearch = new Object[3];
        dataSearch[0] = dataClass[1];
        dataSearch[1] = dataClass[2];
        dataSearch[2] = dataClass[6];
        comboCari.setModel(new DefaultComboBoxModel(dataSearch));
    }
    // End of variables declaration

    private void updateComboSupplier() {
        Object data[] = new Object[listSupplier.size()];
        int x = 0;
        for (Supplier supplier : listSupplier) {
            data[x] = supplier.getKode() + " (" + supplier.getNama() + ")";
            x++;
        }
        comboSupplier.setModel(new DefaultComboBoxModel(data));
    }
    // End of variables declaration

    private void updateData() {
        try {
            Date date = new Date();
            textCari.setText("");
            textJatuhTempo.setText("");
            RandomList randomList = new RandomList();
            textTanggal.setText(simpleDateFormat.format(new Date()));
            textHargaBeli.setText("");
            textJumlah.setText("");
            textTotal.setText("");
            textKode.setText("P" + simpleDateFormat1.format(date) + randomList.getGeneratePassword());
            updateTableItemNota();
            updateTotal();
            listSupplier = supplierDAO.getAll(Supplier.class);
            updateComboSupplier();
            listBarang = barangDAO.getAll(Barang.class);
            updateComboBarang();
            updateComboCari();
        } catch (Exception ex) {
            Logger.getLogger(TransaksiPembelian.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableItemNota() {
        String[] judul = {"No", "Kode", "Nama", "Satuan", "Jumlah", "Harga", "Sub Total"};
        Object[][] isi = new Object[listItemPembelian.size()][7];
        int x = 0;
        int no = 0;
        for (ItemPembelian itemPembelian : listItemPembelian) {
            isi[x][0] = no += 1;
            isi[x][1] = itemPembelian.getBarang().getKode();
            isi[x][2] = itemPembelian.getBarang().getNama();
            isi[x][3] = itemPembelian.getBarang().getSatuan().getNama();
            isi[x][4] = itemPembelian.getJumlah();
            isi[x][5] = "Rp " + numberFormat.format(itemPembelian.getHarga()) + ",00";
            isi[x][6] = "Rp " + numberFormat.format(itemPembelian.getHarga() * itemPembelian.getJumlah()) + ",00";
            x++;
        }
        tableItemPembelian.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        TableColumn column0 = tableItemPembelian.getColumnModel().getColumn(0);
        column0.setPreferredWidth(50);
        column0.setMaxWidth(50);
        column0.setCellRenderer(new CenterRenderer());
        TableColumn column1 = tableItemPembelian.getColumnModel().getColumn(1);
//        column1.setPreferredWidth(100);
//        column1.setMaxWidth(100);
        column1.setCellRenderer(new CenterRenderer());
        TableColumn column3 = tableItemPembelian.getColumnModel().getColumn(3);
//        column3.setPreferredWidth(75);
//        column3.setMaxWidth(75);
        column3.setCellRenderer(new CenterRenderer());
        TableColumn column4 = tableItemPembelian.getColumnModel().getColumn(4);
//        column4.setPreferredWidth(140);
//        column4.setMaxWidth(140);
        column4.setCellRenderer(new CenterRenderer());
        TableColumn column5 = tableItemPembelian.getColumnModel().getColumn(5);
//        column5.setPreferredWidth(140);
//        column5.setMaxWidth(140);
        column5.setCellRenderer(new RightRenderer());
        TableColumn column6 = tableItemPembelian.getColumnModel().getColumn(6);
//        column5.setPreferredWidth(140);
//        column5.setMaxWidth(140);
        column6.setCellRenderer(new RightRenderer());
    }

    private void updateTotal() {
        double total = 0;
        for (ItemPembelian itemPembelian : listItemPembelian) {
            total += itemPembelian.getHarga() * itemPembelian.getJumlah();
        }
        totals = total;
        textTotal.setText("Rp " + numberFormat.format(total) + ",00");

    }
}
